#!/usr/bin/python3
# coding=utf-8
"""
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: YangYunYi
# Create: 2020-11-04
"""

import os
import autoimport.logmodule.logger as logger

this_logger = logger.init('getrequires')
logger.add_file_handler(this_logger, "getrequires", "getrequires")

CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))


def get_file_path(sub_dir, file_name):
    """
    根据子目录与文件拼接处文件路径
    :param sub_dir:
    :param file_name:
    :return:
    """
    if str(sub_dir) != "":
        sub_dir = os.path.join(CURRENT_PATH, sub_dir)
        file_path = os.path.join(sub_dir, file_name)
    else:
        file_path = os.path.join(CURRENT_PATH, file_name)
    return file_path


def get_input_pkg_list(file_name):
    # 获取输入的所有包
    if not os.path.isfile(file_name):
        this_logger.error(
            "Not found the inputPackageList.txt file:%s.", file_name)
        return None
    input_pkg = []
    with open(file_name, "r") as conf_file:
        for line in conf_file:
            input_pkg.append(line.strip())
        message = "    ".join(input_pkg)
        this_logger.debug("Init package list :%s.", message)
    return input_pkg


def list_unique(input_list):
    """
    list去重
    :param input_list: 待处理list
    :return: 去重之后的list
    """
    ret_list = list(set(input_list))
    ret_list.sort(key=input_list.index)
    return ret_list
